var express = require('express');
var app = express();
var fs = require("fs");

//用户列表
app.get('/listUsers', function(req, res){
    fs.readFile(__dirname+'/users.json', 'utf8', function(err, data){
        console.log(data);
        res.end(data);
    });
});

// 新增用户
app.get('/addUser', function(req, res){
    //添加的新用户数据
    let user = {
       "user4" : {
          "name" : "mohit",
          "password" : "password4",
          "profession" : "teacher",
          "id": 4
       }
    }
    fs.readFile(__dirname+'/users.json', 'utf8', function(err, data){
        data = JSON.parse(data);
        data["user4"] = user["user4"];
        console.log(data);
        res.end(JSON.stringify(data));
    });
});

//用户详情
app.get('/user/:id', function(req, res){
    fs.readFile(__dirname+'/users.json', 'utf8', function(err, data){
        data = JSON.parse(data);
        var user = data["user" + req.params.id] 
        console.log( req.params );
        console.log( user );
        res.end( JSON.stringify(user));
    });
});

app.get("/", function(req, res) {
//req.render("index", { message: "Hello World" });
  res.sendfile('./restful/views/index.html');
});



var server = app.listen(8081, function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log("应用实例，访问地址为 http://%s:%s", host, port);
    
});
